package com.yehui.algorithm.sword;

/**
 * Created by XuChunH on 2016/9/16.
 */
public class BTree2LinkedList {

    public TreeNode convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null){
            return null;
        }
        TreeNode head = pRootOfTree;
        if (pRootOfTree.left != null){
            TreeNode root = convert(pRootOfTree.left);
            head = root;
            while (root.right != null){
                root = root.right;
            }
            root.right = pRootOfTree;
            pRootOfTree.left = root;
        }
        if(pRootOfTree.right != null){
            TreeNode root = convert(pRootOfTree.right);
            pRootOfTree.right = root;
            root.left = pRootOfTree;
        }
        return head;
    }

}
